<?php
/**
 * This adapter is a bridge between Ldap supplied data and the application database
 */
class Application_Ldap_Identity_Adapter extends BRail_LdapZendAuthTiein_Identity_Adapter_Ldap
{
    /**
     * Get User object for the identified user.
     *
     * Look up user in user table thanks to UserTable using supplied $login.
     * Saves it in database so we make sure we have the latest information about the user
     * 
     * @param string $login
     * @return User
     */
    public function getIdentity($login)
    {
        $ldapUser = parent::getIdentity($login);

        $table = new UserTable();

        $user = $table->findActiveRowByLogin($login);
        if (null === $user) {
            return null; // No active user found
        }

        $user->firstname = $ldapUser->firstname;
        $user->lastname = $ldapUser->name;
        $user->email = BRail_Mail::cleanInternalEmail($ldapUser->mail);

        // /!\ If no language is defined in LDAP record, it will be nl by default
        $user->language = $ldapUser->language == 'F' ? 'fr' : 'nl';

        $user->save(); /* Ensure that we have the latest version of user information in database */

        return $user;
    }
}